package com.esa.doxml;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.xml.sax.SAXException;

import com.esa.javabean.Choice;

public class CreateScoreXML {
	private OutPutXML opx=null;
	public File createFile(List<Choice> list) throws IOException, SAXException{
		
		opx=new OutPutXML();
		opx.setFilename("C:\\Users\\LiYan\\Desktop\\xml\\C\\choice\\choice.xml");
		
		Element root = DocumentHelper.createElement("choices");
		Element from=root.addElement("from");
		from.setText("C");
		
		Element to=root.addElement("to");
		String str=list.get(0).getCno();
		char cha=str.charAt(0);
		if(cha=='2'){
			to.setText("B");
		}else if(cha=='1'){
			to.setText("A");
		}else{
			System.out.println("CreateChoiceXML line 36...");
		}
		Element order=root.addElement("order");
		order.setText("score");
		  
		Document document = DocumentHelper.createDocument();
		document.setRootElement(root);
		  for (Choice c : list) {
		   root.add(getXML(c));
		  }
		  
		return opx.outPut(root);  
	}
	
	private Element getXML(Choice c){
		
		Document doc=DocumentHelper.createDocument();
		Element root=doc.addElement("choice");
		
		Element cno=root.addElement("Cno");
		cno.setText(c.getCno());
		
		Element sno=root.addElement("Snm");
		sno.setText(c.getSno());
		
		Element s=root.addElement("Grd");
		s.setText(c.getScore());
		
		
		return root;
	}
}
